// pages/yulan/yulan.js
Page({

  data: {
    yulan_status: 0, //0预览 1是审核中
    name: "",
    legal: "",
    region: "",
    contact_name: "",
    contact_mobile: "",
    product_name: "",
    product_cover: [],
    product_description: [],
    logo: [],
    company_introduction: [],
  },

  onLoad(options) {
    var that = this;
    const yulan_status = options.yulan_status;
    that.setData({
      yulan_status: yulan_status
    })
    if (yulan_status == 1) {
      this.getNet();
    } else {
      this.getStorage();
    }

  },
  getNet() {
    var that = this;
    wx.request({
      url: 'https://api.dianshiyexiao.com/weapp/company/info',
      method: 'POST',
      header: {
        'Authorization': wx.getStorageSync('token') // 默认值
      },
      success(res) {
        console.log(res.data)
        if (res.data.data.info) {
          //渲染数据
          that.setData({
            name: res.data.data.info.name,
            legal: res.data.data.info.legal,
            region: res.data.data.info.region,
            contact_name: res.data.data.info.contact_name,
            contact_mobile: res.data.data.info.contact_mobile,
            product_name: res.data.data.info.product_name,
            product_cover: that.stringToImgList(res.data.data.info.product_cover),
            product_description: that.stringToImgList(res.data.data.info.product_description),
            logo: that.stringToImgList(res.data.data.info.logo),
            company_introduction: that.stringToImgList(res.data.data.info.company_introduction),
          })
          wx.setNavigationBarTitle({
            title: that.data.product_name,
          })
        } else {

        }
      },
      fail(error) {
        console.log(error)
      },
    })
  },
  getStorage() {
    var that = this;
    wx.getStorage({
      key: 'yulan',
      success(res) {
        console.log(res.data)
        that.setData(res.data)
        wx.setNavigationBarTitle({
          title: that.data.product_name,
        })
      }
    })
  },
  fabu() {
    var that = this;
    wx.showModal({
      title: '提交审核',
      content: '确认提交审核么?',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          that.fabuNet();
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  fabuNet() {
    var that = this;
    //var json=JSON.stringify(that.data)
    //console.log(json)
    //console.log(that.listUrlToString(that.data.product_cover))
    //if (true) return;
    var updata = {
      name: that.data.name,
      legal: that.data.legal,
      region: that.data.region,
      contact_name: that.data.contact_name,
      contact_mobile: that.data.contact_mobile,
      product_name: that.data.product_name,
      product_cover: that.listUrlToString(that.data.product_cover),
      product_description: that.listUrlToString(that.data.product_description),
      logo: that.listUrlToString(that.data.logo),
      company_introduction: that.listUrlToString(that.data.company_introduction),
    }
    if(that.data.id>0){
      updata.id=that.data.id;
    }
    wx.request({
      url: 'https://api.dianshiyexiao.com/weapp/company/submit',
      method: 'POST',
      data: updata,
      header: {
        'Authorization': wx.getStorageSync('token') // 默认值
      },
      success(res) {
        console.log(res.data)
        if (res.data.status == 200) {
          wx.showToast({
            title: '发布成功,请等待审核',
            icon: "none",
          })
          wx.setStorageSync("yulan", "")
          //wx.navigateBack()
          wx.reLaunch({
            url: '/pages/index1/index1',
          })
        } else {
          wx.showToast({
            title: res.data.data.message,
            icon: "none",
          })
        }
      },
      fail(error) {
        console.log(error)
      },
    })
  },
  listUrlToString(list) {
    var xx = "";
    for (let i = 0; i < list.length; i++) {
      console.log(i);
      console.log(list[i].url);
      xx += list[i].url
      if (i < list.length - 1) {
        xx += ","
      }
    }
    return xx;
  },
  stringToImgList(str) {
    var list = [];
    var array = str.split(",")
    array.forEach(item => {
      list.push({
        "url": item
      })
    });
    return list;
  }


})